//
//  RankForRegisterViewController.swift
//  btvideo
//
//  Created by mac on 2020/11/3.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SwiftyAttributes

class ST_WaitRegisterQueueController: ST_BTLightBarViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        view.addSubview(rank_title_label)
        rank_title_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(44+30+statusBarHeight)
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(37)
        }
        
        view.addSubview(bottom_label)
        bottom_label.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-40-BTSafeAreaBottom)
            make.centerX.equalToSuperview()
            make.height.equalTo(20)
        }
        
        
        view.addSubview(sub_title_label)
        sub_title_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(44+117+statusBarHeight)
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(22)
        }
        
        view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(44+149+statusBarHeight)
            make.left.equalToSuperview().offset(15)
        }
        
        view.addSubview(timer_background_view)
        timer_background_view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.equalTo(345)
            make.height.equalTo(115)
            make.top.equalTo(content_label.snp.bottom).offset(60)
        }
        
        
        timer_background_view.addSubview(minutes_label)
        minutes_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(5)
            make.width.equalTo(84)
            make.height.equalTo(70)
        }
        
        
        timer_background_view.addSubview(separate_label0)
        separate_label0.snp.makeConstraints { (make) in
            make.centerY.equalTo(minutes_label).offset(-4)
            make.width.equalTo(14)
            make.height.equalTo(70)
            make.right.equalTo(minutes_label.snp.left)
        }
        
        timer_background_view.addSubview(hours_label)
        hours_label.snp.makeConstraints { (make) in
            make.right.equalTo(separate_label0.snp.left)
            make.width.height.top.equalTo(minutes_label)
        }
        
        timer_background_view.addSubview(separate_label1)
        separate_label1.snp.makeConstraints { (make) in
            make.centerY.equalTo(separate_label0)
            make.width.equalTo(14)
            make.height.equalTo(70)
            make.left.equalTo(minutes_label.snp.right)
        }
        
        timer_background_view.addSubview(seconds_label)
        seconds_label.snp.makeConstraints { (make) in
            make.left.equalTo(separate_label1.snp.right)
            make.width.height.top.equalTo(minutes_label)
        }
        
        
        
        timer_background_view.addSubview(time_scribe_label0)
        time_scribe_label0.snp.makeConstraints { (make) in
            make.centerX.equalTo(hours_label)
            make.top.equalTo(hours_label.snp.bottom).offset(5)
            make.height.equalTo(20)
        }
        
        timer_background_view.addSubview(time_scribe_label1)
        time_scribe_label1.snp.makeConstraints { (make) in
            make.centerX.equalTo(minutes_label)
            make.top.equalTo(minutes_label.snp.bottom).offset(5)
            make.height.equalTo(20)
        }
        
        timer_background_view.addSubview(time_scribe_label2)
        time_scribe_label2.snp.makeConstraints { (make) in
            make.centerX.equalTo(seconds_label)
            make.top.equalTo(seconds_label.snp.bottom).offset(5)
            make.height.equalTo(20)
        }
        
        timer = Timer.init(timeInterval: 1, target: self, selector: #selector(countdownHandle), userInfo: nil, repeats: true)
        
        RunLoop.current.add(timer!, forMode: RunLoop.Mode.common)
        
        countdownHandle()
        
    }
    
    
    
    
    @objc func countdownHandle(){
        
        
        
        if delay_seconds < 0 {
            
            timer?.invalidate()
            
            timer = nil
            
            return
        }
        
        hours_label.text = String(format: "%02d", delay_seconds/60/60)
        
        minutes_label.text = String(format: "%02d", (delay_seconds.float/60.0).int%60)
        
        seconds_label.text = String(format: "%02d", delay_seconds%60)
        
        delay_seconds -= 1
        
    }
    
    
    //    MARK: - Properties
    var delay_seconds = 10*60
    
    fileprivate var timer:Timer?
    
    fileprivate lazy var rank_title_label:UILabel = {
        let control = UILabel.init()
        control.text = "排队等待注册中..."
        control.textColor = UIColor(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 26, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    fileprivate lazy var sub_title_label:UILabel = {
        let control = UILabel.init()
        control.text = "尊敬的用户："
        control.textColor = UIColor(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.numberOfLines = 0
        
        let p = NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 4)
        
        let str = "当前注册人数过多，为保证大家获得最佳体验，\n请在倒计时结束后进行注册。".withAttributes([Attribute.font(UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium)),Attribute.textColor(UIColor.init(hex: 0x333333)!),.paragraphStyle(p)])
        
        control.attributedText = str
        
        return control
    }()
    
    fileprivate lazy var hours_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 50, weight: UIFont.Weight.semibold)
        
        //control.backgroundColor = .random
        return control
    }()
    
    fileprivate lazy var time_scribe_label0:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.text = "HOURS"
        return control
    }()
    
    
    fileprivate lazy var separate_label0:UILabel = {
        let control = UILabel.init()
        control.text = ":"
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 50, weight: UIFont.Weight.semibold)
        
        
        //control.backgroundColor = .random
        return control
    }()
    
    fileprivate lazy var minutes_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 50, weight: UIFont.Weight.semibold)
        //control.backgroundColor = .random
        return control
    }()
    
    fileprivate lazy var time_scribe_label1:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.text = "MIN"
        return control
    }()
    
    fileprivate lazy var separate_label1:UILabel = {
        let control = UILabel.init()
        control.text = ":"
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 50, weight: UIFont.Weight.semibold)
        
        //control.backgroundColor = .random
        return control
    }()
    
    fileprivate lazy var seconds_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 50, weight: UIFont.Weight.semibold)
        
        //control.backgroundColor = .random
        return control
    }()
    
    fileprivate lazy var time_scribe_label2:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.text = "SEC"
        return control
    }()
    
    
    fileprivate lazy var timer_background_view:ST_FWGradientView = {
        let control = ST_FWGradientView()
        control.bgLayer.colors = [UIColor.init(hex: 0xFFD111)!.cgColor,UIColor.init(hex: 0xFBBE00)!.cgColor]
        control.bgLayer.startPoint = CGPoint(x: 0, y: 0)
        control.bgLayer.endPoint = CGPoint(x: 0, y: 1)
        control.cornerRadius = 15
        return control
    }()
    
    fileprivate lazy var bottom_label:UILabel = {
        let control = UILabel.init()
        control.textColor = UIColor.init(hex: 0x333333)
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.text = "给您带来的不便，我们深表歉意！"
        return control
    }()
    
    
    
}
